// 设置基地址
const baseUrl = "https://meituan.thexxdd.cn/api"
import * as base64 from "base-64"
import * as utf8 from "utf8"

// 获取token'
function getToken() {
    const token = wx.getStorageSync("wxuser").user_Token || "";
    // 对token进行utf8编码
    const utftoken = utf8.encode((token + ':'))
    // 进行baset64加密
    const basetoken = base64.encode(utftoken)
    return "Basic " + basetoken
}

getToken()

export default function request(url, method = "get", obj = {}) {
    return new Promise((resolve, reject) => {
        uni.request({
            url: baseUrl + url,
            method: method,
            data: obj,
            header: {
                "Authorization": getToken()
            },
            success: (res) => {
                switch (res.statusCode) {
                    case 200:
                        resolve(res.data);
                        break;
                    case 404: {
                        wx.showToast({
                            title: '页面走丢了',
                            icon: 'error',
                            duration: 2000
                        })
                        break;
                    }

                    case 202: {
                        wx.showToast({
                            title: res.data.msg,
                            icon: 'error',
                            duration: 2000
                        })
                        break;
                    }
                    case 401: {
                        wx.showToast({
                            title: '没有权限',
                            icon: 'error',
                            duration: 2000
                        })
                        wx.navigateTo({url: '/pages/login/login'})
                        break;
                    }
                }
            },
            fail: (err) => {
                reject(err)
            }
        })

    })
}
